package dk.modified.reshary;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.Toast;

public class VideoActivity extends Activity {

	private CustomVideoView reshareVideo;
	private Dialog diag;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_video);
		
		diag = ProgressDialog.show(this, getString(R.string.loading_video),
				getString(R.string.please_wait), true);

		String filename = getIntent().getExtras().getString("videofilename");

		reshareVideo = (CustomVideoView) findViewById(R.id.reshare_video);

		MediaController mediaController = new MediaController(this);
		mediaController.setAnchorView(reshareVideo);
		mediaController.setMediaPlayer(reshareVideo);

		reshareVideo.setMediaController(mediaController);
		reshareVideo.setOnErrorListener(new OnErrorListener() {
			@Override
			public boolean onError(MediaPlayer mp, int what, int extra) {

				Toast.makeText(getApplicationContext(),
						getString(R.string.video_error), Toast.LENGTH_LONG)
						.show();
				finish();
				return false;
			}
		});

		reshareVideo.setOnPreparedListener(new OnPreparedListener() {
			@Override
			public void onPrepared(MediaPlayer mp) {
				diag.dismiss();
				mp.start();
			}
		});

		reshareVideo.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				finish();
			}
		});

		Uri videoUrl = Uri.parse(filename);
		reshareVideo.setVideoURI(videoUrl);
	}
}
